<?php

namespace app\common\model;

use Org\Util\HelpUtil;
use think\Db;

class ShopPromotionCardItemModel extends BaseModel
{

    protected $name = 'shop_promotion_card_item'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    //读取器
    protected function getBeginDateAttr($value, $data)
    {
        if ($data['date_type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
            return date('Y-m-d', $data['begin_timestamp']);
        }
        if ($data['date_type'] == 'DATE_TYPE_FIX_TERM') {
            return date('Y-m-d', (time() + $data['fixed_begin_term'] * 60 * 60 * 24));
        }
    }

    protected function getEndDateAttr($value, $data)
    {
        if ($data['date_type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
            return date('Y-m-d', $data['end_timestamp']);
        }
        if ($data['date_type'] == 'DATE_TYPE_FIX_TERM') {
            return date('Y-m-d', time() + ($data['fixed_begin_term'] + $data['fixed_term']) * 60 * 60 * 24);
        }
    }

    /**
     * 优惠券详情列表
     * @param string $type unused未使用且可用 expire未使用已过期 used已使用
     * @param $p
     * @param $pagesize
     * @param string $and
     * @return mixed
     */
    public function getPageList($type = 'unused', $pageSize = 12, $map = [], $query = [])
    {
        $time     = time();
        $day_time = 60 * 60 * 24;
        if ($type == 'unused') {  //未使用可用
            $list = $this->alias('t1')
                ->join("shop_promotion_card t2", 't1.card_id=t2.card_id')
                ->where("case date_type
                    when 'DATE_TYPE_FIX_TIME_RANGE' then end_timestamp>{$time}
                    else (receive_time+(fixed_begin_term+fixed_term)*{$day_time})>={$time}
                end")
                ->where("is_use", 0)
                ->where('status',1)
                ->where($map)
                ->paginate($pageSize, false, ['query' => $query]);
        }
        if ($type == 'used') { //已使用
            $list = $this->alias('t1')
                ->join("shop_promotion_card t2", 't1.card_id=t2.card_id')
                ->where("is_use", 1)
                ->where($map)
                ->paginate($pageSize, false, ['query' => $query]);
        }
        if ($type == 'expire') {  //未使用已过期
            $list = $this->alias('t1')
                ->join("shop_promotion_card t2", 't1.card_id=t2.card_id')
                ->where("case date_type
                    when 'DATE_TYPE_FIX_TIME_RANGE' then end_timestamp<{$time}
                    else (receive_time+(fixed_begin_term+fixed_term)*{$day_time})<{$time}
                end")
                ->where("is_use", 0)
                ->where($map)
                ->paginate($pageSize, false, ['query' => $query]);
        }
        return $list;
    }

    /**
     * 优惠券详情列表
     * @param string $type unused未使用且可用 expire未使用已过期 used已使用
     * @param $memberUid 会员ID
     * @param $siteId -1表示全部站点
     * @param $orderMoney 订单金额 大于0生效
     * @param $pageSize 分页条数
     * @return mixed
     */
    public function getMyCardList($type = 'unused', $memberUid, $siteId = -1, $orderMoney = 0, $pageSize = 12)
    {
        $time     = time();
        $day_time = 60 * 60 * 24;
        if ($siteId != -1) {
            $map['site_id'] = $siteId;
        }
        if ($orderMoney > 0) {
            $map['card_order_money'] = ['<=', $orderMoney];
        }
        if ($type == 'unused') {  //未使用可用
            $list = $this->alias('t1')
                ->join("shop_promotion_card t2", 't1.card_id=t2.card_id')
                ->where("case date_type
                    when 'DATE_TYPE_FIX_TIME_RANGE' then end_timestamp>{$time}
                    else (receive_time+(fixed_begin_term+fixed_term)*{$day_time})>={$time}
                end")
                ->where("is_use", 0)
                ->where("member_uid", $memberUid)
                ->where($map)
                ->paginate($pageSize);
        }
        if ($type == 'used') { //已使用
            $list = $this->alias('t1')
                ->join("shop_promotion_card t2", 't1.card_id=t2.card_id')
                ->where("is_use", 1)
                ->where("member_uid", $memberUid)
                ->where($map)
                ->paginate($pageSize);
        }
        if ($type == 'expire') {  //未使用已过期
            $list = $this->alias('t1')
                ->join("shop_promotion_card t2", 't1.card_id=t2.card_id')
                ->where("case date_type
                    when 'DATE_TYPE_FIX_TIME_RANGE' then end_timestamp<{$time}
                    else (receive_time+(fixed_begin_term+fixed_term)*{$day_time})<{$time}
                end")
                ->where("is_use", 0)
                ->where("member_uid", $memberUid)
                ->where($map)
                ->paginate($pageSize);
        }
        $webConfig= cache('webconfig'.$siteId);
        foreach ($list as $index => $value) {
            if (empty($value['card_image'])){
                $list[$index]['card_image']=$webConfig['NOPIC'];
            }
        }
        return $list;
    }

    /**
     * 获取8位随机码
     * @return string
     */
    public function getCardKey()
    {
        $cardKey         = HelpUtil::randString(8, 1);
        $map['card_key'] = $cardKey;
        $map['is_use']   = 0;
        $haveCardKey     = $this->where($map)->value('card_key');
        if ($haveCardKey) {
            $this->getCardKey();
        } else {
            return $cardKey;
        }
    }

    /**
     * 还原15分钟内未付款的卡卷
     * @param $cardId
     */
    public function restoreInventory($cardId)
    {
        $count=$this->where(['card_id'=>$cardId,'receive_time'=>['<',time()-900],'status'=>0])->delete();
        $model=new ShopPromotionCardModel();
        $model->where('card_id',$cardId)->setDec('receive_num',$count);
    }
}